package com.lp.guoguo.util;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.math.BigDecimal;
import java.util.HashMap;

public class TronUtil {

    public static   void main(String[] args) {
//        String transfer = transfer("TP7WxL9DSeXUEB5STm9ZCG4j1Qci3jZXoU");
//        String transfer = transfer(0,20,"TEf5y1PYmCjepVUuHdqQmG1Kd5RJvKo62m");
//        JSONObject jsonObject = JSONObject.parseObject(transfer);
        String account = account("TB2f62SGU7yqtqzL5LCzfH71bA6oXbp9Ff");
        System.out.println("account = " + account);
        JSONObject jsonObject = JSONObject.parseObject(account);
        System.out.println("jsonObject = " + jsonObject);
        //
        JSONArray withPriceTokens = jsonObject.getJSONArray("withPriceTokens");
        for (int i = 0; i < withPriceTokens.size(); i++) {
            JSONObject withPriceToken = withPriceTokens.getJSONObject(i);
            String tokenAbbr = withPriceToken.getString("tokenAbbr");
            BigDecimal balance = withPriceToken.getBigDecimal("balance").divide(new BigDecimal("1000000"));
//            System.out.println("withPriceToken = " + withPriceToken);

            System.out.println("钱包地址 = " + tokenAbbr+"-----钱包余额 = " + balance);



        }
//        jsonObject.getJSONArray("withPriceTokens").getJSONObject(0).getBigDecimal("balance");


//        System.out.println(transfer);

    }

    //TEf5y1PYmCjepVUuHdqQmG1Kd5RJvKo62m
    //TB2f62SGU7yqtqzL5LCzfH71bA6oXbp9Ff

    /**
     * 获取 trx&trc10 转账列表
     * @param start 页码起始
     * @param limit 多少条
     * @param address 钱包地址
     * @return
     */
    public static String transfer(Integer start,Integer limit,String address){
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://apilist.tronscanapi.com/api/transfer";

        MultiValueMap map = new LinkedMultiValueMap();
        map.add("sort","-timestamp");
        map.add("count","true");
        map.add("limit",limit.toString());
        map.add("start",start.toString());
        map.add("address",address);
        map.add("filterTokenValue","1");
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
                .queryParams(map);
        System.out.println("请求地址 = " + builder.toUriString());
        ResponseEntity<String> forEntity = restTemplate.getForEntity(builder.toUriString(), String.class);

        return forEntity.getBody();
    }

    /**
     * 通过交易哈希获取交易详情
     * @param hash
     * @return
     */
    public static String transactionInfo(String hash){
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://apilist.tronscanapi.com/api/transaction-info";

        MultiValueMap map = new LinkedMultiValueMap();
        map.add("hash",hash);
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
                .queryParams(map);
        System.out.println("请求地址 = " + builder.toUriString());
        ResponseEntity<String> forEntity = restTemplate.getForEntity(builder.toUriString(), String.class);

        return forEntity.getBody();
    }


    /**
     * 获取账户详细信息
     * @param address
     * @return
     */
    public static String account(String address){
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://apilist.tronscanapi.com/api/accountv2";

        MultiValueMap map = new LinkedMultiValueMap();
        map.add("address",address);
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
                .queryParams(map);
        System.out.println("请求地址 = " + builder.toUriString());
        ResponseEntity<String> forEntity = restTemplate.getForEntity(builder.toUriString(), String.class);

        return forEntity.getBody();
    }



}
